﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BZ.Common;
using BZ.Common.FileUtility;
using System.IO;

namespace RegexGen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            tp_1.TabPages[0].Text = "RegexGenDemo";
            tp_1.TabPages[1].Text = "RegexTagUrlGetDemo";
            tp_1.TabPages[2].Text = "RegexDomainDemo";
            tp_1.TabPages[3].Text = "RegexTagAttiValueGetDemo";
        }

        private void btn_go_Click(object sender, EventArgs e)
        {
            tb_reg.Text = RegexUtility.GetPairTagInnerRegexStr(tb_tagname.Text, tb_attiname.Text, tb_attivalue.Text);
        }

        private void btn_getalink_Click(object sender, EventArgs e)
        {
            tb_alink.Text = RegexUtility.GetUrlFromTag(tb_ahtm.Text, tb_baselink.Text);
        }

        private void btn_getdomain_Click(object sender, EventArgs e)
        {
            tb_domain.Text = RegexUtility.GetDomainNameByUrl(tb_url.Text);
        }

        private void btn_getValue_Click(object sender, EventArgs e)
        {
            tb_valueforvalue.Text = RegexUtility.GetAttiValueFromTag(tb_taghtmlforValue.Text, tb_attinameforvalue.Text);
        }


        private void btn_writefile_Click(object sender, EventArgs e)
        {
            string errorMsg = null;
            TextFileHelper myFile = TextFileHelper.CreateTextFileHelper(
                tb_sourcefilepath.Text,
                out errorMsg,
                FileAccess.Read,
                Encoding.UTF8);

            if (!string.IsNullOrEmpty(errorMsg))
            {
                MessageBox.Show("文件错误:" + errorMsg);
                return;
            }

            string mySourceText = string.Empty;
            ResultInfo myResult = myFile.ReadAll(out mySourceText);
            if (myResult.IsSuccess)
            {
                int timeOut = 200;
                try
                {
                    timeOut = Convert.ToInt32(tb_timeout.Text);
                }
                catch { MessageBox.Show("超时格式错误，使用默认200ms"); }

                bool isTimeOut = false;
                List<string> myMatchs = RegexUtility.RegexMatcher(
                    new RegexUtility.RegexParam(mySourceText, tb_reg.Text, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline, timeOut),
                    out isTimeOut);
                if (isTimeOut)
                {
                    MessageBox.Show("正则处理超时");
                }
                else if (myMatchs == null || myMatchs.Count <= 0)
                {
                    MessageBox.Show("正则无数据");
                }
                else
                {
                    TextFileHelper myTFile = TextFileHelper.CreateTextFileHelper(
                            tb_targetfilepath.Text,
                            out errorMsg,
                            FileAccess.Write,
                            Encoding.UTF8);
                    myTFile.Delete();
                    myTFile.OpenFile();
                    foreach (string myItem in myMatchs)
                    {
                        myTFile.Write(myItem, true);
                        myTFile.Write("", true);
                        myTFile.Write("****************************", true);
                        myTFile.Write("", true);
                    }
                    myTFile.CloseFile();
                    MessageBox.Show("处理完成");
                }
            }
            else
            {
                MessageBox.Show("读取失败");
            }


        }


    }
}
